/* Create adoption definition of earlier years 1999-2005, as suggested by referees in ReSTAT */
clear all
set more off
cd "" //targed directory



********** HIMSS data
***Year 1999-2000: NO Medicare number
clear
odbc list

foreach YEAR of num 1999/2000 {
clear
qui odbc query "`YEAR'", dialog(complete)
qui odbc load, table("IHDS Facility Look-up")
duplicates drop FacilityID FacilityType,force
tempfile lookup
save `lookup'

clear
qui odbc load, table("IHDS Facility Information")
rename IDFacility FacilityID
merge 1:1 FacilityID using `lookup'
keep if _merge==3
drop _merge
tempfile hinfo
save `hinfo'

clear
odbc load, table("IHDS Facilities Application Table")
merge m:1 FacilityID using `hinfo'
keep if _merge==3
drop _merge
keep if Application=="CLINICAL DATA REPOSITORY"
*di "`YEAR'"
tab FacilityType


gen sftvnd=0
replace sftvnd=2 if strpos(strlower(SoftwareVendor),"self-developed")>0
replace sftvnd=3 if strpos(strlower(SoftwareVendor),"cerner")>0
replace sftvnd=4 if strpos(strlower(SoftwareVendor),"cpsi")>0
replace sftvnd=5 if strpos(strlower(SoftwareVendor),"dairyland")>0 |strpos(strlower(SoftwareVendor),"integrated health systems")>0
replace sftvnd=6 if strpos(strlower(SoftwareVendor),"eclipsys")>0
replace sftvnd=7 if strpos(strlower(SoftwareVendor),"epic")>0
replace sftvnd=8 if strpos(SoftwareVendor,"GE ")>0 | strpos(SoftwareVendor,"IDX")>0 
replace sftvnd=9 if strpos(strlower(SoftwareVendor),"healthcare management systems")>0|strpos(strlower(SoftwareVendor),"health management systems")>0
replace sftvnd=10 if strpos(strlower(SoftwareVendor),"mckesson")>0
replace sftvnd=11 if strpos(strlower(SoftwareVendor),"siemens")>0|strpos(strlower(SoftwareVendor),"sms")>0
replace sftvnd=12 if strpos(strlower(SoftwareVendor),"meditech")>0
replace sftvnd=13 if SoftwareVendor~=""& sftvnd==0

tab Status
gen adp=0
replace adp=1 if strpos( Status,"utomated")>0| strpos( Status,"AUTOMATED")>0
replace adp=0 if strpos( Status,"NOT AUTOMATED")>0| strpos(Status,"Not Automated")>0
replace adp=0 if strpos(SoftwareVendor,"NOT AUTOMATED")>0
gen vnd=adp*sftvnd

keep if FacilityType==1
duplicates drop FacilityID vnd,force
bys FacilityID:gen num=_N
tab num
bys FacilityID:egen maxadp=max(adp)
bys FacilityID:egen minvnd=min(vnd)
bys FacilityID:egen maxvnd=max(vnd)
drop if adp<maxadp&num>1
drop num
bys FacilityID:gen num=_N
drop if vnd>minvnd&num>1&vnd==13
drop num
bys FacilityID:gen num=_N
drop if num>1&minvnd==2&vnd>2
drop num maxadp minvnd maxvnd
bys FacilityID:gen num=_N
tab num

gen year=`YEAR'
replace vnd=1 if vnd==0
gen zipcode=substr(Zip,1,5)
keep FacilityID year vnd Status ContractDate FacilityName Adrs1 Adrs2 City State zipcode BedSize NumOfPhysicians
tempfile adp`YEAR'
save `adp`YEAR''
}



***Year 2001: Yes with Medicare number, but renamed as "var5"
clear
odbc list

foreach YEAR of num 2001 {
clear
qui odbc query "`YEAR'", dialog(complete)
qui odbc load, table("IHDS Facility Look-up")
duplicates drop FacilityID FacilityType,force
tempfile lookup
save `lookup'

clear
qui odbc load, table("IHDS Facility Information")
rename IDFacility FacilityID
merge 1:1 FacilityID using `lookup'
keep if _merge==3
drop _merge
tempfile hinfo
save `hinfo'

clear
odbc load, table("IHDS Facilities Application Table")
merge m:1 FacilityID using `hinfo'
keep if _merge==3
drop _merge
keep if Application=="CLINICAL DATA REPOSITORY"
di "`YEAR'"
tab FacilityType
tab FacilityType if var5>0

gen sftvnd=0
replace sftvnd=2 if strpos(strlower(SoftwareVendor),"self-developed")>0
replace sftvnd=3 if strpos(strlower(SoftwareVendor),"cerner")>0
replace sftvnd=4 if strpos(strlower(SoftwareVendor),"cpsi")>0
replace sftvnd=5 if strpos(strlower(SoftwareVendor),"dairyland")>0 |strpos(strlower(SoftwareVendor),"integrated health systems")>0
replace sftvnd=6 if strpos(strlower(SoftwareVendor),"eclipsys")>0
replace sftvnd=7 if strpos(strlower(SoftwareVendor),"epic")>0
replace sftvnd=8 if strpos(SoftwareVendor,"GE ")>0 | strpos(SoftwareVendor,"IDX")>0 
replace sftvnd=9 if strpos(strlower(SoftwareVendor),"healthcare management systems")>0|strpos(strlower(SoftwareVendor),"health management systems")>0
replace sftvnd=10 if strpos(strlower(SoftwareVendor),"mckesson")>0
replace sftvnd=11 if strpos(strlower(SoftwareVendor),"siemens")>0|strpos(strlower(SoftwareVendor),"sms")>0
replace sftvnd=12 if strpos(strlower(SoftwareVendor),"meditech")>0
replace sftvnd=13 if SoftwareVendor~=""& sftvnd==0

tab Status
gen adp=0
replace adp=1 if strpos( Status,"utomated")>0| strpos( Status,"AUTOMATED")>0
replace adp=0 if strpos( Status,"NOT AUTOMATED")>0| strpos(Status,"Not Automated")>0
replace adp=0 if strpos(SoftwareVendor,"NOT AUTOMATED")>0
gen vnd=adp*sftvnd


keep if FacilityType==1
duplicates drop FacilityID vnd,force
bys FacilityID:gen num=_N
tab num
bys FacilityID:egen maxadp=max(adp)
bys FacilityID:egen minvnd=min(vnd)
bys FacilityID:egen maxvnd=max(vnd)
drop if adp<maxadp&num>1
drop num
bys FacilityID:gen num=_N
drop if vnd>minvnd&num>1&vnd==13
drop num
bys FacilityID:gen num=_N
drop if num>1&minvnd==2&vnd>2
drop num maxadp minvnd maxvnd
bys FacilityID:gen num=_N
tab num


rename (var5 var7) (MedicareNumber ImplementationDate)
gen year=`YEAR'
replace vnd=1 if vnd==0
gen zipcode=substr(Zip,1,5)
keep FacilityID year vnd Status ContractDate FacilityName Adrs1 Adrs2 City State zipcode BedSize NumOfPhysicians MedicareNumber ImplementationDate
tempfile adp`YEAR'
save `adp`YEAR''
}



***Year 2002: Yes with Medicare number, but renamed as "var3"
clear
odbc list

foreach YEAR of num 2002 {
clear
qui odbc query "`YEAR'", dialog(complete)
qui odbc load, table("IHDS Facility Look-up")
duplicates drop FacilityID FacilityType,force
tempfile lookup
save `lookup'

clear
qui odbc load, table("IHDS Facility Information")
merge 1:1 FacilityID using `lookup'
keep if _merge==3
drop _merge
tempfile hinfo
save `hinfo'

clear
odbc load, table("IHDS Facilities Application Table")
merge m:1 FacilityID using `hinfo'
keep if _merge==3
drop _merge
keep if Application=="CLINICAL DATA REPOSITORY"
di "`YEAR'"
tab FacilityType
tab FacilityType if var3>0


gen sftvnd=0
replace sftvnd=2 if strpos(strlower(SoftwareVendor),"self-developed")>0
replace sftvnd=3 if strpos(strlower(SoftwareVendor),"cerner")>0
replace sftvnd=4 if strpos(strlower(SoftwareVendor),"cpsi")>0
replace sftvnd=5 if strpos(strlower(SoftwareVendor),"dairyland")>0 |strpos(strlower(SoftwareVendor),"integrated health systems")>0
replace sftvnd=6 if strpos(strlower(SoftwareVendor),"eclipsys")>0
replace sftvnd=7 if strpos(strlower(SoftwareVendor),"epic")>0
replace sftvnd=8 if strpos(SoftwareVendor,"GE ")>0 | strpos(SoftwareVendor,"IDX")>0 
replace sftvnd=9 if strpos(strlower(SoftwareVendor),"healthcare management systems")>0|strpos(strlower(SoftwareVendor),"health management systems")>0
replace sftvnd=10 if strpos(strlower(SoftwareVendor),"mckesson")>0
replace sftvnd=11 if strpos(strlower(SoftwareVendor),"siemens")>0|strpos(strlower(SoftwareVendor),"sms")>0
replace sftvnd=12 if strpos(strlower(SoftwareVendor),"meditech")>0
replace sftvnd=13 if SoftwareVendor~=""& sftvnd==0

tab Status
gen adp=0
replace adp=1 if strpos( Status,"utomated")>0| strpos( Status,"AUTOMATED")>0
replace adp=0 if strpos( Status,"NOT AUTOMATED")>0| strpos(Status,"Not Automated")>0
replace adp=0 if strpos(SoftwareVendor,"NOT AUTOMATED")>0
gen vnd=adp*sftvnd


keep if FacilityType==1
duplicates drop FacilityID vnd,force
bys FacilityID:gen num=_N
tab num
bys FacilityID:egen maxadp=max(adp)
bys FacilityID:egen minvnd=min(vnd)
bys FacilityID:egen maxvnd=max(vnd)
drop if adp<maxadp&num>1
drop num
bys FacilityID:gen num=_N
drop if vnd>minvnd&num>1&vnd==13
drop num
bys FacilityID:gen num=_N
drop if num>1&minvnd==2&vnd>2
drop num maxadp minvnd maxvnd
bys FacilityID:gen num=_N
tab num


rename (var3 var7) (MedicareNumber ImplementationDate)
gen year=`YEAR'
replace vnd=1 if vnd==0
gen zipcode=substr(Zip,1,5)
keep FacilityID year vnd Status ContractDate FacilityName Adrs1 Adrs2 City State zipcode BedSize NumOfPhysicians MedicareNumber ImplementationDate
tempfile adp`YEAR'
save `adp`YEAR''
}



***Year 2003-4: Yes with Medicare number, but renamed as "var3"; ContractDate -> ContractMonth and ContractYear;  ImplementedDate -> ImplementedMonth and ImplementedYear
clear
odbc list

foreach YEAR of num 2003/2004 {
clear
qui odbc query "`YEAR'", dialog(complete)
qui odbc load, table("IHDS Facility Look-up")
duplicates drop FacilityID FacilityType,force
tempfile lookup
save `lookup'

clear
qui odbc load, table("IHDS Facility Information")
merge 1:1 FacilityID using `lookup'
keep if _merge==3
drop _merge
tempfile hinfo
save `hinfo'

clear
odbc load, table("IHDS Facilities Application Table")
merge m:1 FacilityID using `hinfo'
keep if _merge==3
drop _merge
keep if Application=="CLINICAL DATA REPOSITORY"
di "`YEAR'"
tab FacilityType
tab FacilityType if var3>0


gen sftvnd=0
replace sftvnd=2 if strpos(strlower(SoftwareVendor),"self-developed")>0
replace sftvnd=3 if strpos(strlower(SoftwareVendor),"cerner")>0
replace sftvnd=4 if strpos(strlower(SoftwareVendor),"cpsi")>0
replace sftvnd=5 if strpos(strlower(SoftwareVendor),"dairyland")>0 |strpos(strlower(SoftwareVendor),"integrated health systems")>0
replace sftvnd=6 if strpos(strlower(SoftwareVendor),"eclipsys")>0
replace sftvnd=7 if strpos(strlower(SoftwareVendor),"epic")>0
replace sftvnd=8 if strpos(SoftwareVendor,"GE ")>0 | strpos(SoftwareVendor,"IDX")>0 
replace sftvnd=9 if strpos(strlower(SoftwareVendor),"healthcare management systems")>0|strpos(strlower(SoftwareVendor),"health management systems")>0
replace sftvnd=10 if strpos(strlower(SoftwareVendor),"mckesson")>0
replace sftvnd=11 if strpos(strlower(SoftwareVendor),"siemens")>0|strpos(strlower(SoftwareVendor),"sms")>0
replace sftvnd=12 if strpos(strlower(SoftwareVendor),"meditech")>0
replace sftvnd=13 if SoftwareVendor~=""& sftvnd==0

tab Status
gen adp=0
replace adp=1 if strpos( Status,"utomated")>0| strpos( Status,"AUTOMATED")>0
replace adp=0 if strpos( Status,"NOT AUTOMATED")>0| strpos(Status,"Not Automated")>0
replace adp=0 if strpos(SoftwareVendor,"NOT AUTOMATED")>0
gen vnd=adp*sftvnd


keep if FacilityType==1
duplicates drop FacilityID vnd,force
bys FacilityID:gen num=_N
tab num
bys FacilityID:egen maxadp=max(adp)
bys FacilityID:egen minvnd=min(vnd)
bys FacilityID:egen maxvnd=max(vnd)
drop if adp<maxadp&num>1
drop num
bys FacilityID:gen num=_N
drop if vnd>minvnd&num>1&vnd==1
drop num
bys FacilityID:gen num=_N
drop if num>1&minvnd==2&vnd>2
drop num maxadp minvnd maxvnd
bys FacilityID:gen num=_N
tab num


rename var3 MedicareNumber 
gen year=`YEAR'
replace vnd=1 if vnd==0
gen zipcode=substr(Zip,1,5)
keep FacilityID year vnd Status Contract_* Implemented_* FacilityName Adrs1 Adrs2 City State zipcode BedSize NumOfPhysicians MedicareNumber 
tempfile adp`YEAR'
save `adp`YEAR''
}





**********
**********
********** YEAR 2005 and compile all
clear 
set more off


****** Create hospital characteristics: make it PER MCRNUM for merging later on
clear
foreach YEAR of num 1999/2005 {
use "./rawdata/aha_extract`YEAR'.dta",clear

keep id mcrnum mhsmemb sysid cntrl serv bdtot admtot ipdtot mcrdc mcripd mcddc mcdipd births vtot vem ftmdtf mapp8 mloczip
destring mcrnum,force replace
drop if mcrnum==.
*pick the pivotal hospital if one mcrnum correspondes to multiple id---according to bed size
bys mcrnum:egen maxbd=max(bdtot)
bys mcrnum:egen totbd=max(bdtot)
gen zip=substr(mloczip,1,5)    
destring zip,replace force
bys mcrnum:egen zipcode=max(zip*(bdtot==maxbd))
gen teaching_tmp=0    
destring mapp8,replace force
replace teaching_tmp=1 if mapp8==1
bys mcrnum:egen teaching=max(teaching_tmp)
destring cntrl,replace force   
gen nprofit_tmp=0
replace nprofit_tmp=1 if cntrl==21 | cntrl==22 | cntrl==23
bys mcrnum:egen nprofit=max(nprofit_tmp)
gen profit_tmp=0
replace profit_tmp=1 if cntrl==30 | cntrl==31 | cntrl==32 | cntrl==33
bys mcrnum:egen profit=max(profit_tmp)
bys mcrnum:egen totmcddc=total(mcddc)
bys mcrnum:egen totmcrdc=total(mcrdc)
bys mcrnum:egen totadm=total(admtot)
gen perc_mcd=totmcddc/totadm    
gen perc_mcr=totmcrdc/totadm 
destring sysid,force replace
replace sysid=0 if sysid==.
bys mcrnum:egen maxsysid=max(sysid)
gen year=`YEAR'
bys mcrnum:gen num=_N
drop if bdtot<maxbd&num>1
drop num
bys mcrnum:gen num=_N
drop if admtot<totadm&num>1
di "`YEAR'"
duplicates drop mcrnum,force
keep mcrnum year id totbd nprofit profit teaching totadm perc_mcr perc_mcd maxsysid zipcode
rename (totbd totadm maxsysid) (bdtot admtot sysid)
tempfile hinfo`YEAR'
save `hinfo`YEAR''
}




****** YEAR 2006
clear
use "./rawdata/aha_extract2006.dta",clear
drop lon*
keep id mcrnum
tempfile aha
save `aha'

clear
use "./prepare/allbasic.dta",clear
keep if year==2006
keep UniqueId id ahaid year hsa prechs
merge m:1 id using `aha'
keep if _merge==3
drop _merge
tempfile adp2006
save `adp2006'




****** HIMSS YEAR 2005
clear
foreach comp in 31  {
foreach YEAR in 2005 {
clear
odbc list
qui odbc query "`YEAR'", dialog(complete)
qui odbc load, table("HAEntity")
keep HAEntityId UniqueId MedicareNumber HAEntityType HAEntityTypeId 
tempfile haeid
save `haeid'


clear
qui odbc load, table("HAEntityApplication")
merge m:1 HAEntityId using `haeid'
keep if _merge==3
drop _merge
keep UniqueId Application ApplicationId CategoryId Status SoftwareVendorId SoftwareVendorName Contract* Implemented* HAEntityId MedicareNumber HAEntityType HAEntityTypeId
rename (ContractYear ContractMonth ImplementedMonth ImplementedYear) (Contract_Year Contract_Month Implemented_Month Implemented_Year)
keep if CategoryId==6  
keep if ApplicationId==`comp'
drop if HAEntityTypeId==2
gen year=`YEAR'
}
  }


****** Drop observations that are not the actual choice of the hospital
duplicates drop UniqueId SoftwareVendorId,force
duplicates tag UniqueId,gen (dup_uid)
drop if dup_uid>0 & Status=="To be Replaced"
drop dup_uid
duplicates tag UniqueId,gen (dup_uid)
drop if dup_uid>0 & Status=="Not Automated"
drop dup_uid
duplicates tag UniqueId,gen (dup_uid)
drop if dup_uid>0 & Status=="Not Yet Contracted"
drop dup_uid
duplicates tag UniqueId,gen (dup_uid)
bys UniqueId dup_uid:egen impyr=max(ContractYear)
drop if dup_uid>0 & impyr~=ContractYear
drop dup_uid impyr
duplicates tag UniqueId,gen (dupid)


*** The rest obs with dupid>0 are replicate
sort UniqueId SoftwareVendorId
duplicates drop UniqueId,force



****** Re-ID vendor identity
gen adp=(Status=="Live and Operational")
gen vendor=SoftwareVendorId*adp
drop adp
gen vnd=0
replace vnd=1 if vendor==0
replace vnd=2 if vendor==2577
replace vnd=3 if vendor==470
replace vnd=4 if vendor==668
replace vnd=5 if vendor==729
replace vnd=6 if vendor==890
replace vnd=7 if vendor==936
replace vnd=8 if vendor==1062
replace vnd=9 if vendor==1200
replace vnd=10 if vendor==1704
replace vnd=11 if vendor==2613
replace vnd=12 if vendor==5153
replace vnd=13 if vendor~=0 & vendor~=2577 & vendor~=470 & vendor~=668 & vendor~=729 & vendor~=890 & vendor~=936 & vendor~=1062 & vendor~=1200 & vendor~=1704 & vendor~=2613 & vendor~=5153


merge 1:m UniqueId using `adp2006'
** Take out and put aside the hospitals that haven't adopted EMRs by 2006 and missing in the previous years of HIMSS data
preserve
keep if _merge==2&prechs==1
keep UniqueId id ahaid mcrnum MedicareNumber hsa
tempfile noadp
save `noadp'
restore
**
replace vnd=prechs if prechs==1&_merge~=1
preserve
drop if _merge==2
keep UniqueId id ahaid mcrnum MedicareNumber vnd Status hsa
tempfile adp2005
save `adp2005'
restore



** Medicare number --- FacilityID (create the MOST COMPLETE list of "mcrnum" after merging 2005 and 2006, then append using "adp1999-2003" to get as many pairs of "FacilityID---MedicareNumber")
preserve

drop if _merge==2&prechs==1
drop _merge

keep mcrnum 
rename mcrnum MedicareNumber 
tempfile mcr
save `mcr'
restore

preserve
keep MedicareNumber
append using `mcr'
destring MedicareNumber,force replace
duplicates drop MedicareNumber,force
drop if MedicareNumber==.

merge 1:m MedicareNumber using `adp2004'
keep if _merge==3
drop _merge
keep FacilityID MedicareNumber
foreach YEAR of num 2001/2003 {
append using `adp`YEAR''
keep FacilityID MedicareNumber
drop if MedicareNumber==0| MedicareNumber==.
}
duplicates drop FacilityID MedicareNumber,force
tempfile mcr_facID
save `mcr_facID'
restore
**


****** Compile files
****** Years 1999-2004
clear
use `mcr_facID',clear
foreach YEAR of num 1999/2004 {
preserve
merge m:1 FacilityID using `adp`YEAR''
keep if _merge==3
drop _merge
rename MedicareNumber mcrnum
merge m:1 mcrnum using `hinfo`YEAR'',force
keep if _merge==3
drop _merge
bys id:gen num=_N
tab num
bys id:egen minvnd=min(vnd)
drop if vnd>minvnd&minvnd==1&num>1
duplicates drop id vnd,force
drop num
bys id:gen num=_N
drop if num>1
drop num minvnd
tempfile comb`YEAR'
save `comb`YEAR''
restore
}


** Year 2005
clear
use `adp2005',clear
merge m:1 id using `hinfo2005',force
keep if _merge==3
drop _merge
bys id:gen num=_N
tab num
drop num
tempfile comb2005
save `comb2005'


** Those NA by 2006
clear
use `noadp'
gen vnd=1
foreach YEAR of num 1999/2005 {
preserve
merge m:1 id using `hinfo`YEAR'',force
keep if _merge==3
drop _merge 
bys id:gen num=_N
tab num
drop num
tempfile na`YEAR'
save `na`YEAR''
restore
}




****** Combine "comb`YEAR'"
clear
use `comb1999',clear
append using `comb2000',force
append using `comb2001',force
append using `comb2002',force
append using `comb2003',force
append using `comb2004',force
append using `comb2005',force
append using `na1999',force
append using `na2000',force
append using `na2001',force
append using `na2002',force
append using `na2003',force
append using `na2004',force
append using `na2005',force


****** Identify HSA for previous years
rename hsa hsa_tmp
bys id:egen hsa=mode(hsa_tmp)
drop hsa_tmp


****** create numerical format of id: ahaid
destring id,gen(ahaid) force
replace ahaid=6360604 if id=="636060A"
replace ahaid=6361184 if id=="636118A"
replace ahaid=6380804 if id=="638080A"
replace ahaid=6390764 if id=="639076A"
replace ahaid=6530327 if id=="653032B"
replace ahaid=6530904 if id=="653090A"
replace ahaid=6530954 if id=="653095A"
replace ahaid=6540904 if id=="654090A"
replace ahaid=6630154 if id=="663015A"
replace ahaid=6650431 if id=="665043A"
replace ahaid=6710114 if id=="671011A"
replace ahaid=6730504 if id=="673050A"
replace ahaid=6931394 if id=="693139A"


****** Fill in control variables
local varlist "bdtot nprofit profit teaching admtot perc_mcr perc_mcd sysid"

foreach VAR in `varlist' {
*1st
sort ahaid year
bys ahaid:gen l`VAR'=`VAR'[_n-1]
replace `VAR'=l`VAR' if `VAR'==.&l`VAR'~=.
drop l`VAR'
*2nd
sort ahaid year
bys ahaid:gen l`VAR'=`VAR'[_n-1]
replace `VAR'=l`VAR' if `VAR'==.&l`VAR'~=.
drop l`VAR'
*3rd
sort ahaid year
bys ahaid:gen l`VAR'=`VAR'[_n-1]
replace `VAR'=l`VAR' if `VAR'==.&l`VAR'~=.
drop l`VAR'
*4th
sort ahaid year
bys ahaid:gen l`VAR'=`VAR'[_n-1]
replace `VAR'=l`VAR' if `VAR'==.&l`VAR'~=.
drop l`VAR'
*5th
sort ahaid year
bys ahaid:gen l`VAR'=`VAR'[_n-1]
replace `VAR'=l`VAR' if `VAR'==.&l`VAR'~=.
drop l`VAR'
*6th
sort ahaid year
bys ahaid:gen l`VAR'=`VAR'[_n-1]
replace `VAR'=l`VAR' if `VAR'==.&l`VAR'~=.
drop l`VAR'
}


foreach VAR in `varlist' {
*1st
sort ahaid year
bys ahaid:gen f`VAR'=`VAR'[_n+1]
replace `VAR'=f`VAR' if `VAR'==.&f`VAR'~=.
drop f`VAR'
*2nd
sort ahaid year
bys ahaid:gen f`VAR'=`VAR'[_n+1]
replace `VAR'=f`VAR' if `VAR'==.&f`VAR'~=.
drop f`VAR'
*3rd
sort ahaid year
bys ahaid:gen f`VAR'=`VAR'[_n+1]
replace `VAR'=f`VAR' if `VAR'==.&f`VAR'~=.
drop f`VAR'
*4th
sort ahaid year
bys ahaid:gen f`VAR'=`VAR'[_n+1]
replace `VAR'=f`VAR' if `VAR'==.&f`VAR'~=.
drop f`VAR'
*5th
sort ahaid year
bys ahaid:gen f`VAR'=`VAR'[_n+1]
replace `VAR'=f`VAR' if `VAR'==.&f`VAR'~=.
drop f`VAR'
*6th
sort ahaid year
bys ahaid:gen f`VAR'=`VAR'[_n+1]
replace `VAR'=f`VAR' if `VAR'==.&f`VAR'~=.
drop f`VAR'
*7th
sort ahaid year
bys ahaid:gen f`VAR'=`VAR'[_n+1]
replace `VAR'=f`VAR' if `VAR'==.&f`VAR'~=.
drop f`VAR'
*8th
sort ahaid year
bys ahaid:gen f`VAR'=`VAR'[_n+1]
replace `VAR'=f`VAR' if `VAR'==.&f`VAR'~=.
drop f`VAR'
*9th
sort ahaid year
bys ahaid:gen f`VAR'=`VAR'[_n+1]
replace `VAR'=f`VAR' if `VAR'==.&f`VAR'~=.
drop f`VAR'
}


save "./prepare/allbasic_oldyrs",replace








